import Layout from '@/layout/index.vue';
import NotFound from '@/views/404.vue';
import { Setting } from '@element-plus/icons-vue';
import { createRouter, createWebHistory } from 'vue-router';

export const routes = [
  {
    path: '/login',
    component: () => import('@/views/LoginView.vue'),
  },
  {
    path: '/',
    redirect: '/dashboard',
  },
  {
    path: '/dashboard',
    component: Layout,
    children: [
      {
        path: '', // 空路径表示这是默认子路由
        name: 'Dashboard',
        component: () => import('@/views/DashboardView.vue'),
        meta: {
          title: '仪表盘',
          icon: 'Odometer',
        },
      },
    ],
  },
  {
    path: '',
    component: Layout,
    meta: {
      title: '系统管理',
      icon: Setting,
    },
    children: [
      {
        path: 'user',
        component: () => import('@/views/system/UserView.vue'),
        meta: {
          title: '用户管理',
        },
      },
      {
        path: 'role',
        component: () => import('@/views/system/RoleView.vue'),
        meta: {
          title: '角色管理',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
  },
];

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 或其他验证方式
  if (to.path === '/login') {
    next()
    return
  }
  if (!isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router;
